package com.sqllog.admin.auth.service;

import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * Token 记录服务
 *
 * @author chennengcheng
 * @date 2022-01-14 14:35
 * @remark
 */

@Component
public class SecureUserTokenServiceImpl implements PersistentTokenRepository {

    private final static String USERNAME_KEY = "spring:security:rememberMe:username_key:";
    private final static String SERIES_KEY = "spring:security:rememberMe:series_key:";
    public final static Map<String, Map<String, String>> tokenMap = new HashMap<>();
    public final static Map<String, String> seriesMap = new HashMap<>();
    @Override
    public void createNewToken(PersistentRememberMeToken persistentRememberMeToken) {
        String series = persistentRememberMeToken.getSeries();
        String key = generateKey(series, SERIES_KEY);
        String usernameKey = generateKey(persistentRememberMeToken.getUsername(), USERNAME_KEY);
        deleteIfPresent(usernameKey);
        HashMap<String, String> hashMap = new HashMap<>(3);
        hashMap.put("username", persistentRememberMeToken.getUsername());
        hashMap.put("token", persistentRememberMeToken.getTokenValue());
        hashMap.put("date", String.valueOf(persistentRememberMeToken.getDate().getTime()));
        tokenMap.put(key, hashMap);
        seriesMap.put(usernameKey, series);
    }

    @Override
    public void updateToken(String s, String s1, Date date) {
        String key = generateKey(s, SERIES_KEY);
        Map<String, String> oldValueMap = tokenMap.get(key);
        if (Objects.nonNull(oldValueMap)) {
            oldValueMap.put("token", s1);
            tokenMap.put(key, oldValueMap);
        }
    }

    @Override
    public PersistentRememberMeToken getTokenForSeries(String s) {
        String key = generateKey(s, SERIES_KEY);
        Map<String, String> userMap = tokenMap.get(key);
        String username = userMap.get("username");
        String tokenValue = userMap.get("token");
        String date = userMap.get("date");
        if (null == username || null == tokenValue || null == date) {
            return null;
        }

        Long timestamp = Long.valueOf(date);
        Date time = new Date(timestamp);
        return new PersistentRememberMeToken(username, s, tokenValue, time);
    }

    @Override
    public void removeUserTokens(String s) {
        String key = generateKey(s, USERNAME_KEY);
        deleteIfPresent(key);
    }

    private void deleteIfPresent(String key) {
        try {

            if (seriesMap.containsKey(key)) {
                String series = generateKey(seriesMap.get(key), SERIES_KEY);
                if (series != null && seriesMap.containsKey(series)) {
                    seriesMap.remove(series);
                    seriesMap.remove(key);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String generateKey(String series, String prefix) {
        return prefix + series;
    }

}
